<?php
class Clound_Public
{
	/**
     * Singleton instance
     *
     * @var Clound_Public
     */
    protected static $_instance = null;
	/**
     * Returns an instance of Clound_Public
     *
     * Singleton pattern implementation
     *
     * @return Clound_Public Provides a fluent interface
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
        	self::$_instance = new self();
        }

        return self::$_instance;
    }
	public static function getNameByCode($data,$code)
	{
		$sName = '';
		$iTotal = sizeof($data);
		for ($i=0; $i < $iTotal; $i++) { 
			if ($data[$i]['Code'] == $code) {
				$sName = $data[$i]['Name'];
				break;
			}
		}
		return $sName;
	}
	// build the "WHERE" clause depending on the filter's condition, value and datafield.
	public function getClauseByFilter($filtercondition, $filterdatafield, $filtervalue){
		$where = '';
		switch($filtercondition)
        {
            case "NOT_EMPTY":
            case "NOT_NULL":
                $where .= " " . $filterdatafield . " NOT LIKE '" . "" ."'";
                break;
            case "EMPTY":
            case "NULL":
                $where .= " " . $filterdatafield . " LIKE '" . "" ."'";
                break;
            case "CONTAINS_CASE_SENSITIVE":
                $where .= " BINARY " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
                break;
            case "CONTAINS":
                $where .= "" . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
                break;
            case "DOES_NOT_CONTAIN_CASE_SENSITIVE":
                $where .= " BINARY" . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
                break;
            case "DOES_NOT_CONTAIN":
                $where .= "" . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
                break;
            case "EQUAL_CASE_SENSITIVE":
                $where .= " BINARY" . $filterdatafield . " = '" . $filtervalue ."'";
                break;
            case "EQUAL":
                $where .= "" . $filterdatafield . " = '" . $filtervalue ."'";
                break;
            case "NOT_EQUAL_CASE_SENSITIVE":
                $where .= " BINARY" . $filterdatafield . " <> '" . $filtervalue ."'";
                break;
            case "NOT_EQUAL":
                $where .= "" . $filterdatafield . " <> '" . $filtervalue ."'";
                break;
            case "GREATER_THAN":
                $where .= "" . $filterdatafield . " > '" . $filtervalue ."'";
                break;
            case "LESS_THAN":
                $where .= "" . $filterdatafield . " < '" . $filtervalue ."'";
                break;
            case "GREATER_THAN_OR_EQUAL":
                $where .= "" . $filterdatafield . " >= '" . $filtervalue ."'";
                break;
            case "LESS_THAN_OR_EQUAL":
                $where .= "" . $filterdatafield . " <= '" . $filtervalue ."'";
                break;
            case "STARTS_WITH_CASE_SENSITIVE":
                $where .= " BINARY" . $filterdatafield . " LIKE '" . $filtervalue ."%'";
                break;
            case "STARTS_WITH":
                $where .= "" . $filterdatafield . " LIKE '" . $filtervalue ."%'";
                break;
            case "ENDS_WITH_CASE_SENSITIVE":
                $where .= " BINARY" . $filterdatafield . " LIKE '%" . $filtervalue ."'";
                break;
            case "ENDS_WITH":
                $where .= "" . $filterdatafield . " LIKE '%" . $filtervalue ."'";
                break;
        }
        return $where;
	}

    public function getStaffFromStaffId($arrStaff,$id)
    {
        $out = array();
        $iTotal = sizeof($arrStaff);
        for ($i=0; $i < $iTotal; $i++) {
            if ($arrStaff[$i]['StaffID'] == $id) {
                $out = $arrStaff[$i];
                break;
            }
        }
        return $out;
    }
    public function getDepFromUnitID($arrUnit,$id)
    {
        $out = array();
        $iTotal = sizeof($arrUnit);
        for ($i=0; $i < $iTotal; $i++) { 
            if ($arrUnit[$i]['UnitID'] == $id) {
                $out = $arrUnit[$i];
                break;
            }
        }
        return $out;
    }
    public function removeElementArray($data,$key,$value){
        $out = array();
        foreach ($data as $k => $v) {
            if($v[$key] == $value){
                unset($data[$k]);
            }
        }
        array_values($data);
        return $data;
    }
    /*
     * Lay ten tu danh sach ma
     */
    public function getNameByCodeList($datas, $arrCode) {
        $arrName = array();
        if (empty($datas)) {
            return $arrName;
        }
        foreach ($arrCode as $key => $code) {
            foreach ($datas as $k => $data) {
                if ($data['Code'] == $code) {
                    array_push($arrName, $data['Name']);
                    break;
                }
            }
        }
        return $arrName;
    }
    public function _htmlAttribs($attrs){
        if(is_null($attrs))
            return '';
        $htmlAttr = '';
        foreach ($attrs as $key => $value) {
            if (is_array($value)) {
                $value = implode(' ', $value);
            }
            $htmlAttr .= $key.'="'.$value.'" ';
        }
        return $htmlAttr;
    }
}
